<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/24 0024
 * Time: 下午 3:03
 */

namespace App\Model;


use App\Utility\BaseEnum;

class User extends BaseModel
{
    protected $userId;          //用户ID
    protected $userToken;       //用户Token
    protected $userName;        //用户姓名
    protected $face;            //用户头像
    protected $isSpeak;         //用户是否能发言
    protected $extends;         //附加用户信息
    protected $group;           //
    protected $clientIp;        //IP
    
    //DB对象
    public $tableName = 'qc_user';
    public function __construct(){
        parent::__construct();
    }
    
    /**
     * TODO 获取用户信息
     * @param array $wusIds
     * @param array $fields
     * @param boolean $lock
     */
    public function getUserById($userIds,$fields, $lock = FALSE){
        if(!$userIds || empty($userIds)) return false ;
        $result = false ;
        if ($lock === true) {
            $result = $this->getDb()->where('uid',$userIds,'in')->selectForUpdate(TRUE)->get($this->tableName,1000,$fields);
        }else{
            $result = $this->getDb()->where('uid',$userIds,'in')->get($this->tableName,1000,$fields);
        }
        $this->clearPool(); //释放连接池
        return $result;
    }

    public function getUserByUid($uid,$fields=[]){
        if(!$uid || empty($uid)) return false ;
        $result = $this->getDb()->where('uid',$uid)->getOne($this->tableName,$fields);
        return $result;
    }
    
    /**
     * TODO 获取代理信息
     * @param int $userId 代理ID
     * @param string $fields
     * @return array 数组对象 
     */
    public function getAgentById($userId,$fields = FALSE) {
        if(!$fields) $fields = '*';
        $result = $this->getDb()->where('uid',$userId)->where('group_id',2)->getOne($this->tableName,$fields);
        $this->clearPool(); //释放连接池
        return $result;
    }
    
    
    /**
     * TODO 获取用户信息
     */
    public function getUser($playname,$fields = false){
        if(!$fields) $fields = '*';
        $result = $this->getDb()->where('name',$playname)->getOne($this->tableName,$fields);
        return $result;
    }
    
    //TODO 获取单个信息
    public function getUserOneFields($userId,$field) {
        if(!$userId || intval($userId)<= 0) return false ;
        $result = $this->getDb()->where('uid',$userId)->getOne($this->tableName,$field);
        $this->clearPool(); //释放连接池
        if(isset($result) && !empty($result)){
            return isset($result[$field]) ? $result[$field] : FALSE ;
        }
    }
    
    
    //TODO 机器人列表
    public function getRobotsList($agentId,$fields = false,$num = 10) {
        $result = $this->getDb()->where('recUid',$agentId)->where("isNation",3)->orderBy("name","DESC")->get($this->tableName,$num,$fields);
        $this->clearPool(); //释放连接池
        return $result ?? FALSE;
    }
    
    
    //TODO 插入数据
    public function insertUser($userInfo){
        $this->getDb()->insert($this->tableName, $userInfo);
        $lastUid = $this->getDb()->getInsertId();
        $this->clearPool(); //释放连接池
        return $lastUid ?? FALSE;
    }
    
    //TODO 插入数据列表
    public function insertListUser($userList){
        $allIds = $this->getDb()->insertMulti($this->tableName, $userList);
        $this->clearPool(); //释放连接池
        return $allIds ?? FALSE;
    }
}



